<?php
/**
 * 框架的生命周期回调 - 统一定义
*/

namespace houze;

class Lifecycle {

    /**
     * 当Worker进程Start完成时的回调
    */
    public static function worker_start() {
        $obj = new \houze\lifecycle\worker_start();
        $obj->run();
        return true;
    }

    /**
     * 当应用层捕捉到错误时，系统回调处理的生命周期
    */
    public static function controller_error($e, $error, $source) {
        $obj = new \houze\lifecycle\controller_error();
        $obj->run($e, $error, $source);
        unset($obj);
        return false;
    }
    
    /**
     * 推送失败的生命周期回调
    */
    public static function websocket_push_error($server, $content, $fd) {
        $obj = new \houze\lifecycle\websocket_push_error();
        $obj->run($server, $content, $fd);
        unset($obj);
        return false;
    }

    /**
     * 注解解析完成后的生命周期回调
    */
    public static function route_start($route) {
        $obj = new \houze\lifecycle\route_start();
        $obj->run($route);
        unset($obj);
        return true;
    }

    /**
     * 当注解Param检测失败时，回调的处理函数
    */
    public static function annotate_param($server, $fd, $callback, $tips, $name, $status, $attach=null) {
        $obj = new \houze\lifecycle\annotate_param();
        $obj->run($server, $fd, $tips, $name, $status, $attach);
        unset($obj);
        return true;
    }

    /**
     * 当检测失败时，回调的处理函数
    */
    public static function rpc_error($config, $status) {
        $obj = new \houze\lifecycle\rpc_error();
        $obj->run($config['class'], $config['function'], $config, $status);
        return false;
    }

    /**
     * 当Mysql连接池数小于等于0时，回调的通知函数
    */
    public static function mysql_pop_error($type) {
        $obj = new \houze\lifecycle\mysql_pop_error();
        $obj->run($type);
        return false;
    }

    /**
     * 单元测试注解的回调处理
    */
    public static function testcase_callback($tips) {
        $obj = new \houze\lifecycle\testcase_callback();
        $obj->run($tips);
        return false;
    }

    /**
     * 当Redis连接池数小于等于0时，回调的通知函数
    */
    public static function redis_pop_error($type) {
        $obj = new \houze\lifecycle\redis_pop_error();
        $obj->run($type);
        return false;
    }

    /**
     * 当其余注解检测失败时，回调的处理函数
    */
    public static function route_error($server, $fd, $status) {
        $obj = new \houze\lifecycle\route_error();
        $obj->run($server, $fd, $status);
        return false;
    }

    /**
     * JWT注解检测失败时，回调的处理函数
    */
    public static function jwt_error($status) {
        $obj = new \houze\lifecycle\jwt_error();
        $obj->run($status);
        return false;
    }

    /**
     * CSRF注解检测失败时，回调的处理函数
    */
    public static function csrf_error($status) {
        $obj = new \houze\lifecycle\csrf_error();
        $obj->run($status);
        return false;
    }

    /**
     * 路由限流器达到峰值时，回调的通知函数
    */
    public static function limit_route($server, $fd, $callback, $server_type, $route, $data) {
        $obj = new $callback;
        $obj->run($server, $fd, $server_type, $route, $data);
        return false;
    }

    /**
     * IP限流器达到峰值时，回调的通知函数
    */
    public static function limit_ip($server, $fd, $callback, $server_type, $ip, $data) {
        $obj = new $callback;
        $obj->run($server, $fd, $server_type, $ip, $data);
        return false;
    }

    /**
     * 验证器注解检测失败时，回调的处理函数
    */
    public static function validate_error($server_type, $batch, $errors, $callback) {
        $obj = new $callback;
        $obj->run($server_type, $batch, $errors);
        return false;
    }
}